package com.wg.db.schema;

import java.sql.Timestamp;

/**
 * 用于定义各种数据类型的枚举值，并提供类型转换功能
 *
 * @author 少爷123
 */
public enum FieldType {
    /**
     * 未知类型
     */
    Unknown((byte) 0),
    /**
     * boolean
     */
    Boolean((byte) 1),
    /**
     * byte
     */
    Byte((byte) 3),
    /**
     * byte[]
     */
    ByteArray((byte) 4),
    /**
     * short
     */
    Short((byte) 5),
    /**
     * float
     */
    Float((byte) 7),
    /**
     * double
     */
    Double((byte) 9),
    /**
     * int
     */
    Integer((byte) 11),
    /**
     * long
     */
    Long((byte) 13),
    /**
     * date
     */
    Date((byte) 14),
    /**
     * String
     */
    String((byte) 15),
    /**
     * object
     */
    Object((byte) 16);

    private final byte value;

    FieldType(byte value) {
        this.value = value;
    }

    public static FieldType valueOf(byte value) {
        switch (value) {
            case 0:
                return Unknown;
            case 1:
                return Boolean;
            case 3:
                return Byte;
            case 4:
                return ByteArray;
            case 5:
                return Short;
            case 7:
                return Float;
            case 9:
                return Double;
            case 11:
                return Integer;
            case 13:
                return Long;
            case 14:
                return Date;
            case 15:
                return String;
            case 16:
                return Object;
            default:
                return null;
        }
    }

    public static FieldType valueOf(Class<?> type) {
        if (type == boolean.class || type == Boolean.class) {
            return FieldType.Boolean;
        } else if (type == byte.class || type == Byte.class) {
            return FieldType.Byte;
        } else if (type == byte[].class || type == Byte[].class) {
            return FieldType.ByteArray;
        } else if (type == short.class || type == Short.class) {
            return FieldType.Short;
        } else if (type == int.class || type == Integer.class) {
            return FieldType.Integer;
        } else if (type == float.class || type == Float.class) {
            return FieldType.Float;
        } else if (type == long.class || type == Long.class) {
            return FieldType.Long;
        } else if (type == Double.class || type == double.class) {
            return FieldType.Double;
        } else if (type == String.class) {
            return FieldType.String;
        } else if (type == java.util.Date.class || type == Timestamp.class) {
            return FieldType.Date;
        } else {
            return FieldType.Object;
        }
    }

    public byte getValue() {
        return value;
    }
}
